package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;

import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseTmVo;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.baomidou.mybatisplus.extension.api.R;
import org.redisson.RedissonSubList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/list")
public class ListApiController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private SearchService searchService;
    /**
     * 创建索引
     * @return
     */
    @GetMapping("inner/createIndex")
    public Result creadteIndex(){
        //调用工具类创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //设置索引库映射信息
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 删除索引
     * @return
     */
     @GetMapping("inner/deleteIndex")
    public Result deleteIndex(){
        elasticsearchRestTemplate.deleteIndex("goods1");
        return Result.ok();
     }

    /**
     * 构建商品索引库文档对象Goods,将文档存入索引库
     * @param skuId
     * @return
     */
     @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGood(@PathVariable("skuId") Long skuId){
       searchService.upperGoods(skuId);
         return Result.ok();
     }

    /**
     * 删除索引库文档
     * @param skuId
     * @return
     */
     @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId){
             searchService.lowerGoods(skuId);
         return Result.ok();
     }

    /**
     * 更新商品的热度排名
     * @param skuId
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public void innerScore(@PathVariable("skuId")Long skuId){
         searchService.innerScore(skuId);
    }

    /**
     * 商品的检索
     * @param searchParam
     * @return
     */
     @PostMapping("/")
    public Result search(@RequestBody SearchParam searchParam){
         SearchResponseVo responseVo= searchService.search(searchParam);
         return Result.ok(responseVo);
     }
}
